import {
  conbineHolderList,
  tokenHolder
} from '../../config/apiServe/new-rpc.js'
export default {
  data() {
    return {
      holderInfo: {
        total: Number(process.env.VUE_APP_COINS_TOTAL),
        symbol: process.env.VUE_APP_BASECOIN
      },
      HolderList: [],
      listParam: [{
        page: {
          number: 1,
          size: 10
        },
        "sort": [{
          "key": "balance",
          "ascending": false
        }],
        range: [{
          key: 'balance',
          gt: 0
        }],
        "match": [{
          "value": process.env.VUE_APP_BASECOIN,
          "key": "asset_symbol"
        }]
      }],
      pages: {
        number: 1,
        size: 10
      }
    }
  },
  mounted() {
    this.init()
  },
  methods: {
    init() {
      this.getHolderCount()
      this.getHolderList(this.pages)
    },
    // 获取持有人总数
    async getHolderCount() {
      let param = [{
        match: [{
          value: process.env.VUE_APP_BASECOIN,
          key: 'asset_symbol'
        }],
        range: [{
          key: 'balance',
          gt: 0
        }]
      }]
      const res = await tokenHolder([param])
      // this.holderInfo.holders = res.result[0]
      this.$set(this.holderInfo, 'holders', res.result[0])
    },
    // 获取持有人列表
    async getHolderList(pages) {
      this.listParam[0].page = pages
      let list = await conbineHolderList(this.listParam)
      if (list.error === null) {
        let total = this.holderInfo.total
        this.HolderList = list.result
        this.HolderList.forEach((item, index) => {
          item.rank = index + (this.pages.number - 1) * this.pages.size + 1
          item.percent = (item.balance / (total * 1000000)).toFixed(8).slice(0, -6)
        })
      }
    },
    pageChange(page) {
      this.pages.number = page;
      this.getHolderList(this.pages)
    },
    sizeChange(size) {
      this.pages.size = size;
      this.pages.number = 1;
      this.getHolderList(this.pages)
    }
  }
}